<?php
namespace app\admin\controller;
use think\Request;
use think\Controller;
<<<<<<< HEAD
class Addtable extends Controller
{
    public function Addtable()
    {
  		
		return $this->fetch('admin/addtable');
    }
=======
use app\admin\model\Sfenlei;
use app\admin\model\Shangpin;
class Addtable extends Controller
{
	//加载添加商品页面
    public function addtable()
    {
    	//查出所有的父级类
		    	$html = '';
		    	$lei = $this->allFenlei();
		    	foreach ($lei as $value) {
		    		$id = $value->id;
		    		$name = $value->fenlei;
		    		$html .= "<option value='$id'>$name(父类)</option>";
		    	}
	    //查出优惠卷的信息
		    	$shangP = new Sfenlei();
		    	$juan = $shangP->select3();
		    $this->assign('juan',$juan);	
	    	$this->assign('lei',$lei);
	    	$this->assign('html',$html);
		return $this->fetch('admin/addtable');
    }
    //查询点击后的对应父级的子类
    public function allZilei(Request $request)
    {
    	$arr = [];
    	$html = '<option value="s">以下是子类</option>';
    	$prev_id = $request::instance()->param('prev_id');
    	$type = $request::instance()->param('type');
    	$result = $this->allFenlei($prev_id);
    	if ($result) {
    		foreach ($result as $value) {
	    		$id = $value->id;
	    		$name = $value->fenlei;
	    		$html .= "<option value='$id'>$name(子类)</option>";
	    	}
	    	if ($type == 'aaa') {
	    		$html = "<div style='margin-left:0px;margin-top:1px'>
		          			<select class='form-control leinum' style='width:250px;' value=$prev_id name='splei'>
					            $html
		         			</select>	
        				</div>";
	    	}else{
	    		$html = "<div style='margin-left:0px;margin-top:1px'>
		          			<select class='leinum2' style='width:250px;' value=$prev_id name='splei'>
					            $html
		         			</select>	
        				</div>";
	    	}
    	}else{
    		$html = '';
    	}
    	$arr['html'] = $html;
    	$arr['parent_id'] = $prev_id;
    	return $arr;	
    }
    //查询出对应商品的字类
    public function allFenlei($fid = null)
    {
    	$shangP = new Sfenlei();
    	return $shangP->select2($fid);
    }
    //增加商品类
    public function addsplei(Request $request)
    {
    	if (!$request::instance()->param('prev_id')) {
    		return false;
    	}
    	$data = [
    				'prev_id'	=>	$request::instance()->param('prev_id'),
    				'fenlei'	=>	$request::instance()->param('fenlei'),
    	];
    	$shangP = new Sfenlei();
    	$result = $shangP->insert2($data);
    	if ($result) {
    		return true;
    	}else{
    		return false;
    	}
    	//return $shang->insert2();
    }
    //添加商品
    public function addSpin(Request $request)
    {	
    	$shangP = new Shangpin();
    	$result = $request::instance()->param();
    	//插入商品表信息
    		//判断关键信息是否为空
    			if (empty($result['shangpin'])) {
    				$this->error('请填写商品名');
    			}elseif(empty($result['keywords'])){
    				$this->error('请填写商品关键字');
    			}elseif (empty($result['shengchan'])) {
    				$this->error('请填写商品生产日期');
    			}elseif (empty($result['num'])) {
    				$this->error('请填写商品总量');
    			}elseif (empty($result['yuanjia'])) {
    				$this->error('请填写商品价格');
    			}elseif (empty($result['jianjie'])) {
    				$this->error('请填写商品简介');
    			}elseif (empty($result['splei'])||($result['splei'] == 's')) {
    				$this->error('请选择商品所属分类');
    			}
    		//获取商品图片
    			$images = $this->imagesup($request);
    	$arr = [
    			'shangpin'		=>	$result['shangpin'],
    			'keywords'		=>	$result['keywords'],
    			'jianjie'		=>	$result['jianjie'],
    			'sfenlei_id'			=>	$result['splei'],
    			'shengchan'		=>	$result['shengchan'],
    			'baozhiqi'		=>	$result['baozhiqi'],
    			'chandi'		=>	$result['chandi'],
    			'chucunwendu'	=>	$result['chucunwendu'],
    			'baozhuang'		=>	$result['baozhuang'],
    			'num'			=>	$result['num'],
    			'yuanjia'		=>	$result['yuanjia'],
    			'jifen'			=>	$result['jifen'],
    			'img1'			=>	$images[0],
    			'img2'			=>	$images[1],
    			'img3'			=>	$images[2],
    	];
    	//获取新增商品的id
    		$sid = $shangP->insert2($arr);
    	//插入商品优惠劵表信息
    		//是否存在优惠劵
	    		if (empty($result['juan'])) {
	    			$juanid = 0;
	    		}else{
	    			$juanid = join($result['juan']);
	    		}
	    		$juandas = [
	    					'sid' 		=> $sid,
	    					'juan_id'	=> $juanid,
	    		];
	    		$shangP->insertYh($juandas);
    	//插入商品活动表信息
    		//是否存在活动
	    		if (empty($result['huodong'])) {
	    			$status = 1;
	    		}else{
	    			$status = join($result['huodong']);
	    		}
	    		//判断是否存在优惠劵
	    		if (empty($juanid)) {
		    		$datas = [
		    					'shangpin_id'	=>	$sid,
		    					'status'		=>	'1' . $status,
		    				];
	    		}else{
	    			$datas = [
		    					'shangpin_id'	=>	$sid,
		    					'status'		=>	'1' . $status . '5',
		    				];
	    		}
    		$shangP->insertHd($datas);
    		$this->redirect('Addtable/addtable');
    }
    //处理商品图片上传
    public function imagesup(Request $request)
    {
		// 获取表单上传文件
			$files = $request->file('image');
			$item = [];
		foreach($files as $file){
		// 移动到框架应用根目录/public/uploads/ 目录下
			$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
			if($info){
				$item[] = $info->getRealPath();
			}else{
		// 上传失败获取错误信息
			$this->error($file->getError());
			}
		}
		//$this->success('文件上传成功'.implode('<br/>',$item));
		return $item;
	}
>>>>>>> 30d92055e9c414d4b857afd0613e979a055c3b0c
}